package com.db.williamchartdemo.slice;

import com.db.williamchart.extensions.AttrUtils;
import com.db.williamchart.slidertooltip.SliderTooltip;
import com.db.williamchart.view.*;
import com.db.williamchartdemo.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.utils.Pair;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world ability slice
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final long animationDuration = 1000;

    private List<Pair<String,Float>> barSet;
    private List<Pair<String,Float>> horizontalBarSet;
    private List<Pair<String,Float>> lineSet;
    private List<Float> donutSet;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

//        DirectionalLayout directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_dlContainer);
        Text lineChartTitle = (Text) findComponentById(ResourceTable.Id_lineChartTitle);
        Text barChartTitle = (Text) findComponentById(ResourceTable.Id_barChartTitle);
        Text lineChartValue = (Text) findComponentById(ResourceTable.Id_lineChartValue);
        Text lineChartUnits = (Text) findComponentById(ResourceTable.Id_lineChartUnits);
        Text appLogo = (Text) findComponentById(ResourceTable.Id_appLogo);
        initList();

        File file = AttrUtils.resPathToFile(getContext(),"open_sans_bold.ttf");
        Font.Builder builder = new Font.Builder(file);
        Font labelsFont = builder.build();

        lineChartTitle.setFont(labelsFont);
        barChartTitle.setFont(labelsFont);
        lineChartValue.setFont(labelsFont);
        lineChartUnits.setFont(labelsFont);
        appLogo.setFont(labelsFont);

        BarChartView barChartView = (BarChartView) findComponentById(ResourceTable.Id_barChart);
        DonutChartView donutChart = (DonutChartView) findComponentById(ResourceTable.Id_donutChart);
        HorizontalBarChartView horizontalBarChart = (HorizontalBarChartView) findComponentById(ResourceTable.Id_horizontalBarChart);
        LineChartView lineChart = (LineChartView) findComponentById(ResourceTable.Id_lineChart);

        lineChart.setGradientFillColors(new int[]{
                Color.getIntColor("#66FFFFFF"),
                Color.getIntColor("#00FFFFFF")
        });
        lineChart.tooltip = new SliderTooltip();
        lineChart.setAnimationDuration(animationDuration);
        lineChart.setOnDataPointTouchListener(new AxisChartView.OnDataPointTouchListener() {
            @Override
            public void onDataPointTouch(int index, Float x, Float y) {
                lineChartValue.setText(lineSet.get(index).s.toString());
            }
        });
        lineChart.animate(lineSet);

        barChartView.setAnimationDuration(animationDuration);
        barChartView.animate(barSet);

        donutChart.setDonutColors(new int[]{
                Color.getIntColor("#FFFFFFFF"),
                Color.getIntColor("#9EFFFFFF"),
                Color.getIntColor("#8DFFFFFF")
        });
        donutChart.setAnimationDuration(animationDuration);
        donutChart.animate(donutSet);

        horizontalBarChart.setAnimationDuration(animationDuration);
        horizontalBarChart.animate(horizontalBarSet);
    }


    private void initList(){
        barSet = new ArrayList<>();
        barSet.add(new Pair<>("JAN",4F));
        barSet.add(new Pair<>("FEB",7F));
        barSet.add(new Pair<>("MAR",2F));
        barSet.add(new Pair<>("MAY",2.3F));
        barSet.add(new Pair<>("APR",5F));
        barSet.add(new Pair<>("JUN",4F));

        horizontalBarSet = new ArrayList<>();
        horizontalBarSet.add(new Pair<>("PORRO",5F));
        horizontalBarSet.add(new Pair<>("FUSCE",6.4F));
        horizontalBarSet.add(new Pair<>("EGET",3F));

        lineSet = new ArrayList<>();
        lineSet.add(new Pair<>("label1",5F));
        lineSet.add(new Pair<>("label2",4.5F));
        lineSet.add(new Pair<>("label3",4.7F));
        lineSet.add(new Pair<>("label4",3.5F));
        lineSet.add(new Pair<>("label5",3.6F));
        lineSet.add(new Pair<>("label6",7.5F));
        lineSet.add(new Pair<>("label7",7.5F));
        lineSet.add(new Pair<>("label8",10F));
        lineSet.add(new Pair<>("label9",5F));
        lineSet.add(new Pair<>("label10",6.5F));
        lineSet.add(new Pair<>("label11",3F));
        lineSet.add(new Pair<>("label12",4F));


        donutSet = new ArrayList<>();
        donutSet.add(20f);
        donutSet.add(80f);
        donutSet.add(100f);

    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}